<template>
  <div class="budget-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-edit"></i> 金额录入
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :offset="15" :span="3">
            <el-input
              @keyup.enter.native="query"
              placeholder="项目名"
              v-model="queryForm.eventName"
            />
          </el-col>
          <el-col :span="3">
            <el-input
              @keyup.enter.native="query"
              placeholder="团员名"
              v-model="queryForm.memberName"
            />
          </el-col>
          <el-col :span="3">
            <el-button @click="query" icon="el-icon-search" type="primary"
              >搜索
            </el-button>
          </el-col>
        </el-row>
      </div>

      <el-row justify="center" type="flex">
        <el-pagination
          :current-page.sync="pageIndex"
          :page-size="pageSize"
          :total="pageSize * pageCount"
          @current-change="getPage"
          background
          layout="prev, pager, next"
        >
        </el-pagination>
      </el-row>

      <div class="table">
        <el-button @click="export2Excel">导出账单</el-button>
        <el-table :data="tableData" stripe>
          <el-table-column label="项目Id" prop="memberEventId" />
          <el-table-column label="项目名" prop="eventName" />
          <el-table-column label="团员姓名" prop="memberName" />
          <el-table-column label="标价" prop="bidPrice" />
          <el-table-column label="应付" prop="shouldPrice" />
          <el-table-column label="实付" prop="budget" />
          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                @click="edit(scope.row.memberEventId)"
                size="mini"
                type="success"
                >修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <el-form-item label="项目Id">
            <el-input
              disabled
              type="number"
              v-model="entityForm.memberEventId"
            ></el-input>
          </el-form-item>
          <el-form-item label="标价">
            <el-input type="number" v-model="entityForm.bidPrice"></el-input>
          </el-form-item>
          <el-form-item label="应付">
            <el-input type="number" v-model="entityForm.shouldPrice"></el-input>
          </el-form-item>
          <el-form-item label="实付">
            <el-input type="number" v-model="entityForm.budget"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/leader/budget";

export default {
  name: "LeaderBudget",
  data() {
    return {
      queryForm: {
        eventName: "",
        memberName: ""
      },
      entityForm: {},
      tableData: [],
      pageSize: api.pageSize,
      pageCount: 1,
      pageIndex: 1,
      editing: false
    };
  },
  methods: {

    //__________________________________________________________________________----
    query() {
      api
        .getPageCount(this.queryForm.eventName, this.queryForm.memberName)
        .then(res => {
          this.pageCount = res;
          this.pageIndex = 1;
          this.getPage(1);
        });
    },
    export2Excel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../excel/Export2Excel');
        const tHeader = [
          "项目id",
          "项目名",
          "团员姓名",
          "标价",
          "应付",
          "实付"
        ];
        // 上面设置Excel的表格第一行的标题
        const filterVal = [
          "memberEventId",
          "eventName",
          "memberName",
          "bidPrice",
          "shouldPrice",
          "budget"
        ];
        const list = this.tableData;  //把data里的tableData存到list
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '账单');
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getPage(pageIndex) {
      api
        .getPage(
          pageIndex,
          this.queryForm.eventName,
          this.queryForm.memberName
        )
        .then(res => {
          this.tableData = res;
        });
    },
    edit(id) {
      api.get(id).then(res => {
        this.entityForm = res;
        this.editing = true;
      });
    },
    save() {
      api.update(this.entityForm).then(() => {
        this.$message.success("成功");
        this.getPage(this.pageIndex);
        this.editing = false;
      });
    }
  },
  created() {
    this.query();
  }
};
</script>

<style scoped></style>
